package com.youyiwen.Controller;

import com.youyiwen.Bean.Question;
import com.youyiwen.Mapper.QuestionMapper;
import com.youyiwen.Service.CommentService;
import com.youyiwen.Service.QuestionService;
import com.youyiwen.dto.CommentDTO;
import com.youyiwen.dto.QuestionDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;

import javax.print.attribute.standard.Media;
import java.util.List;

/**
 * @Author: zhaoyang
 * @Date: 2021/03/22
 */
@Controller
public class QuestionController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private QuestionMapper questionMapper;


    @GetMapping(value = "/question/{id}")
    public String Question(@PathVariable(name = "id") String id,
                           Model model) {
        QuestionDTO questionDTO = questionService.getById(id);
        //增加阅读数
        questionService.incView(id);
        List<CommentDTO> comments = commentService.commentDTOList(id);
        String userName = questionMapper.selectUserNameById(id);
        List<Question> hisQuestions = questionMapper.selectHotQuestions(userName);
        model.addAttribute("hisQuestions",hisQuestions);
        model.addAttribute("comments", comments);
        model.addAttribute("question", questionDTO);
        return "questions";
    }

    @GetMapping("/delete/question/{id}")
    public String deleteQuestion(@PathVariable String id){
        questionMapper.deleteQuestion(id);
        return "redirect:/userCenter";
    }

}
